<?php

namespace app\common\service;


class ServerUpdateLog extends Base 

{
    function list($params, $limit = 10)
    {
        $db = db('server_update_log')->where('host_id', $params['host_id']);

        //条件筛选
        if(isset($params['old_version']) && !empty($params['old_version'])){
            $db->where('old_version','like','%'.$params['old_version'].'%');
        }
        if(isset($params['new_version']) && !empty($params['new_version'])){
            $db->where('new_version','like','%'.$params['new_version'].'%');
        }
        if(isset($params['ip']) && !empty($params['ip'])){
            $db->where('ip','like','%'.$params['ip'].'%');
        }
        if(isset($params['type']) && !empty($params['type'])){
            $db->where('type', $params['type']);
        }
        if(isset($params['status']) && !empty($params['status'])){
            $db->where('status', $params['status']);
        }
        if(isset($params['desc']) && !empty($params['desc'])){
            $db->where('desc','like','%'.$params['desc'].'%');
        }
        if(isset($params['ctime']) && !empty($params['ctime'])){
            $start_time = strtotime($params['ctime']);
            $end_time = strtotime($params['ctime'].' 23:59:59');
            $db->where('ctime','between', [$start_time, $end_time]);
        }

        $data = $db->order('id desc')->where('status','<>',0)->paginate($limit)->toArray();

        return $data;
    }

    /**
     * @name 更新日志
     * @author wx
     * @date 2020/7/28 14:55
     */
    function hostList($params, $limit = 10)
    {
        $db = db('server_update_log')->alias('sl')
            ->join('host h', 'sl.host_id = h.id', 'left')
            ->group('host_id')
            ->field('sl.host_id, h.name, h.client_version, h.client_update_status');

        if(isset($params['name']) && !empty($params['name'])){
            $db->where('h.name', 'like', '%'.$params['name'].'%');
        }
        if(isset($params['client_version']) && !empty($params['client_version'])){
            $db->where('h.client_version', 'like', '%'.$params['client_version'].'%');
        }
        if(isset($params['client_update_status']) && is_numeric($params['client_update_status'])){
            $db->where('h.client_update_status', $params['client_update_status']);
        }

        $list = $db->paginate($limit)->toArray();

        return $list;
    }

    /**
     * @name 添加
     * @param $addData
     * @return int|string
     * @author wx
     * @date 2019/11/7 16:42
     */
    function add($addData)
    {
        return db('server_update_log')->insert($addData);
    }

    function del($id)
    {
        return db('server_update_log')->where('id','in', $id)->delete();
    }

    function clear()
    {
        return db('server_update_log')->where('id', '>=',1)->delete();
    }
}